/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.MDTOHtml;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.service.ArticleCategoryService;
import com.mxg.blog.service.ArticleService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/6/28 15:52
 * 修改文章内容
 */
@Controller
public class ChangeArticle {
    CqLogger logger = CqLogger.getInstance();

    //注入articleService依赖
    private final ArticleService articleService;
    private final ArticleCategoryService articleCategoryService;
    public ChangeArticle (ArticleService articleService, ArticleCategoryService articleCategoryService) {
        this.articleService = articleService;
        this.articleCategoryService = articleCategoryService;
    }

    @PostMapping("/api/ChangeArticle")
    public ResponseEntity<Map<String, Object>> ChangeArticle(HttpServletRequest request, @RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();
        // 文章ID
        Integer articleId = new Integer(requestMap.get("articleId"));

        // 标题
        String articleTitle = requestMap.get("articleTitle");
        // logger.print(articleTitle);
        // md格式的内容
        String articleContentMd = requestMap.get("articleContentMd");
        // html格式的内容
        MDTOHtml mdtoHtml = MDTOHtml.getInstance(articleContentMd);
        String articleContentHtml = mdtoHtml.toHtmlExtensions();
        // logger.print(articleContent);
        // 文章状态 发布状态
        Integer articleStatus = 1;
        // 标签
        String articleTags = requestMap.get("articleTags");
        // logger.print(articleTags);
        // 是否是原创

        // 封面
        String coverImageUrl = requestMap.get("coverImageUrl");
        // logger.print(coverImageUrl);

        // 摘要
        String articleExcerpt = requestMap.get("articleTextArea");
        // logger.print(articleExcerpt);

        // 分类
        Integer articleCategoryId;
        int articleCate = Integer.parseInt(requestMap.get("articleCate"));
        // logger.print(articleCate);

        // 获取 articleCategory
        HttpSession session = request.getSession();
        // 获取session中articleCategoryNameList的值
        List<String> articleCategoryNameList = (List<String>) session.getAttribute("cateName");
        // logger.print(articleCategoryNameList.get(articleCate));

        // 查找分类id
        try {
            articleCategoryId = articleCategoryService.getArticleCategoryIdByName(
                    articleCategoryNameList.get(articleCate)).getArticleCategoryId();
        }catch (Exception e)
        {
            logger.print("保存失败: " + e);
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_SAVE_ARTICLE);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_SAVE_ARTICLE);
            return ResponseEntity.ok(resultMap);
        }

        try{
            articleService.updateArticle(articleTitle, articleContentHtml, articleContentMd,
                                         articleStatus, articleTags,
                                         coverImageUrl, articleExcerpt,
                                         articleCategoryId, articleId);
        }catch (Exception e)
        {
            logger.print("保存失败: " + e);
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_SAVE_ARTICLE);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_SAVE_ARTICLE);
            return ResponseEntity.ok(resultMap);
        }

        resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
        resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        return ResponseEntity.ok(resultMap);
    }

}
